<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="98px"
    >
      <el-form-item label="分类名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入分类名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button type="danger" size="mini" @click="handleAdd"
          >添加分类</el-button
        >
        <!-- <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button> -->
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="list" style="width: 100%">
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{ (pageNum - 1) * pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="分类名称"
        align="center"
        prop="name"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="分类图标" align="center">
        <template slot-scope="scope">
          <img :src="scope.row.icon" style="width: 60px; height: 60px" />
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="update_time" />
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.show_flag == 0">显示</span>
          <span v-else>隐藏</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleDetail(scope.row)"
            v-hasPermi="['business:classify:list']"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="handleEdit(scope.row)"
            v-hasPermi="['business:classify:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="handleRemove(scope.row)"
            v-hasPermi="['business:classify:remove']"
            >作废</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="pageNum"
      :limit.sync="pageSize"
      @pagination="getList"
    />
    <!-- 添加分类 -->
    <el-dialog
      :title="form.id ? '修改分类' : '新增分类'"
      :visible.sync="dialogFormVisible"
      width="400px"
    >
      <el-form :model="form" :rules="rules" ref="ruleForm">
        <el-form-item label="分类名称" prop="name" label-width="100px">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="分类图标" prop="icon" label-width="100px">
          <el-upload
            class="avatar-uploader"
            action="/admin-api/common/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="form.icon" :src="form.icon" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="排序" label-width="100px">
          <el-input v-model="form.sort" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="显示/隐藏" label-width="100px">
          <el-radio-group v-model="form.show_flag">
            <el-radio :label="0">显示</el-radio>
            <el-radio :label="1">隐藏</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addClassify('ruleForm')"
          >保 存</el-button
        >
      </div>
    </el-dialog>
    <!-- 查看 -->
    <el-dialog title="详情" :visible.sync="detailVisible" width="400px">
      <el-form :model="form">
        <el-form-item label="分类名称：" prop="name" label-width="100px">
          <div v-html="form.name"></div>
        </el-form-item>
        <el-form-item label="分类图标：" prop="icon" label-width="100px">
          <img v-if="form.icon" :src="form.icon" class="avatar" />
        </el-form-item>
        <el-form-item label="排序：" label-width="100px">
          <div v-html="form.sort"></div>
        </el-form-item>
        <el-form-item label="显示/隐藏：" label-width="100px">
          <div v-if="form.show_flag == 0">显示</div>
          <div v-else>隐藏</div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="checkOver">确 认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list, add, remove, detail, edit } from "@/api/material/classify";

export default {
  name: "Classify",
  data() {
    return {
      // 遮罩层
      loading: true,
      dialogFormVisible: false,
      // 详情
      detailVisible: false,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      pageNum: 1,
      pageSize: 10,
      // 查询参数
      queryParams: {
        name: undefined,
        show_flag: undefined,
      },
      form: {
        name: "",
        icon: "",
        sort: 0,
        show_flag: 0,
      },
      rules: {
        name: [{ required: true, message: "请输入分类名称", trigger: "blur" }],
        icon: [{ required: true, message: "请上传分类图标", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 新增
    handleAdd() {
      this.dialogFormVisible = true;
      this.form = {
        id: "",
        name: "",
        icon: "",
        sort: 0,
        show_flag: 0,
      };
    },
    handleAvatarSuccess(file) {
      console.log("分类", file);
      this.form.icon = file.url;
    },
    beforeAvatarUpload() {},
    addClassify(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let method = this.form.id ? edit : add;
          method(this.form).then((response) => {
            this.dialogFormVisible = false;
            this.getList();
            this.msgSuccess(this.form.id ? "修改成功" : "新增成功");
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 查看
    handleDetail(row) {
      detail({ id: row.id }).then((response) => {
        this.detailVisible = true;
        this.form = {
          id: row.id,
          name: row.name,
          icon: row.icon,
          sort: row.sort,
          show_flag: row.show_flag - 0,
        };
      });
    },
    checkOver() {
      this.detailVisible = false;
    },
    // 修改
    handleEdit(row) {
      this.dialogFormVisible = true;
      this.form = {
        id: row.id,
        name: row.name,
        icon: row.icon,
        sort: row.sort,
        show_flag: row.show_flag - 0,
      };
    },
    // 删除
    handleRemove(row) {
      this.$confirm("此操作将作废该分类, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return remove({ id: row.id });
        })
        .then(() => {
          this.getList();
          this.msgSuccess("作废成功！");
        });
    },
    /** 查询登录日志列表 */
    getList() {
      this.loading = true;
      list({
        ...this.queryParams,
        pageNum: this.pageNum,
        pageSize: this.pageSize,
      }).then((response) => {
        this.list = response?.data?.records;
        this.total = response?.data?.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 强退按钮操作 */
    handleForceLogout(row) {
      this.$confirm(
        '是否确认强退名称为"' + row.userName + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return forceLogout(row.tokenId);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("强退成功");
        });
    },
  },
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
